<?php

namespace Caisse\CaisseBundle\Controller;

use Caisse\CaisseBundle\Entity\Serveur;
use Caisse\CaisseBundle\Form\ServeurType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class ServeurController extends Controller
{
        public function loginMobileAction(){
            $request = $this->getRequest();
            $username =$request->get('username');
            $password = $request->get('password');
 
            $em= $this->getDoctrine()->getManager();
            $session = $request->getSession();
            $user = $em->getRepository('CaisseBundle:Serveur')->findOneBy(array("username"=>$username,"password"=>$password));
            $resultat=array();
            ////////////:
            if ($user !=null){
                $session->set('serveur', $user);
                $resultat['username'] = $user->getUsername();
                $resultat['password'] = $user->getPassword();
                $resultat['id'] = $user->getId();
                $resultat['success'] = true;
                 $tables = $em->getRepository("CaisseBundle:Tables")->getTableServeur($user->getId());
        
        $resultat['tables'] = array();
        foreach ($tables as $table){

            $tab = array();
            $tab['numTable']=$table->getNumTable();
            $tab ['id']=$table->getId();
            if ($table->getServeur()!=null)
            {$table->setEtatTable(1);
            $em->persist($table);
             $em->flush();
            $tab ['etatTable']=1;
            
            }else
            {
                $table->setEtatTable(0);
            $em->persist($table);
             $em->flush();
            $tab ['etatTable']=0;
            }
            
            $resultat['tables'][] = $tab;
         
        }
     
                
            }   else{
                $resultat['success'] = false;
            }      
            return new Response(json_encode($resultat));
                
            //if ((count($user)>0) & (count($pass)>0))
              //  return new Response(json_encode(array('success'=> false))) ;
      // return new Response(json_encode(array('success'=> TRUE))) ;
            
        }
    

        public function listServeurAction()
    {
        $em= $this->getDoctrine()->getManager();
        $serveurs = $em->getRepository("CaisseBundle:Serveur")->findAll();
        
        return $this->render('CaisseBundle:Serveur:listServeur.html.twig', array(
            'serveurs' => $serveurs,
        ));
    }
    
    public function ajoutServeurAction()
    {   
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if($id != "")
        $serveur = $em->getRepository('CaisseBundle:Serveur')->find($id);
        else
            $serveur = new Serveur();
        
        $form = $this->createForm(new ServeurType(), $serveur);
        
        
        if ($request->isMethod('POST')) {
            $form->bind($request);
            if ($form->isValid()) {
                $serveur = $form->getData();
                
                
                $repository = $em->getRepository('CaisseBundle:Serveur');
                $em->persist($serveur);
                $em->flush();
                $serveurs = $repository->findAll();
                 return $this->render('CaisseBundle:Serveur:listServeur.html.twig', array('serveurs' => $serveurs));
            }
        }
        return $this->render('CaisseBundle:Serveur:ajoutServeur.html.twig', array('form' => $form->createView(), 'id' => $id));

  
        
    }
    public function modifierServeurAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $serveur = $em->getRepository('CaisseBundle:Serveur')->find($id);
        $form = $this->createForm(new ServeurType(),$serveur);
        print "xxcxcxc";
        if ($request->isMethod('post')) {
            $serveur->bind($request);
            if ($form->isValid()) {
                $serveur = $form->getData();
                $em->persist($serveur);
                $em->flush();
                $url = $this->generateUrl("caisse_list_serveur");
                return $this->redirect($url);
            }
        }
        return $this->render('CaisseBundle:Serveur:ajoutServeur.html.twig', array('form' => $form->createView(), 'id' => $id));

    }
    
    public function supprimerServeurAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getManager();
        $produit = $em->getRepository('CaisseBundle:Serveur')->find($id);
        $em->remove($produit);
        $em->flush();
        $url = $this->generateUrl("caisse_list_serveur");
        return $this->redirect($url);
    }
    public function  loginServeurAction(){
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        
        $username = $this->getRequest()->get('username');
        $password = $this->getRequest()->get('password');
        
        
    }

}
